home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jtkutils.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  9.9 KB  |  324 lines

  1. # jtkutils.tcl - general utilities requiring Tk
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non¡profit, noncommercial use.
  5. ######################################################################
  6.  
  7. ### TO DO
  8. ###   j:rule and j:filler should read defaults for size, colour, etc.
  9.  
  10. ######################################################################
  11. # global variables:
  12. #
  13. global J_PREFS env
  14. if {! [info exists J_PREFS(autoposition)]} {set J_PREFS(autoposition) 0}
  15. if {! [info exists J_PREFS(confirm)]} {set J_PREFS(confirm) 1}
  16. if { ! [info exists J_PREFS(visiblebell)]} {set J_PREFS(visiblebell) 1}
  17. if { ! [info exists J_PREFS(audiblebell)]} {set J_PREFS(audiblebell) 1}
  18. #
  19. ######################################################################
  20.  
  21. ######################################################################
  22. # metawidget options:
  23. #
  24. option add *Rule.relief sunken widgetDefault
  25. option add *Rule.width 2 widgetDefault
  26. option add *Rule.height 2 widgetDefault
  27. option add *Rule.borderWidth 1 widgetDefault
  28. option add *Filler.relief flat widgetDefault
  29. option add *Filler.width 10 widgetDefault
  30. option add *Filler.height 10 widgetDefault
  31.  
  32. ######################################################################
  33. # j:wm_client - set the session client hostname
  34. ######################################################################
  35.  
  36. proc j:wm_client {{hostname USE_HOSTNAME}} {
  37.   if {"x$hostname" == "xUSE_HOSTNAME"} {
  38.     set hostname localhost
  39.     set hostname_cmd FAIL
  40.     foreach pathname {
  41.       /bin/hostname
  42.       /etc/hostname
  43.       /usr/etc/hostname
  44.       /usr/bsd/hostname
  45.       /usr/bin/hostname
  46.       /usr/ucb/hostname
  47.     } {
  48.       if [auto_execok $pathname] {
  49.         set hostname_cmd $pathname
  50.         break
  51.       }
  52.     }
  53.     if {"x$hostname_cmd" == "xFAIL"} {
  54.       j:alert \
  55.         -text "Can't determine hostname; can't find `hostname' to execute."
  56.     } else {
  57.       if [catch {exec hostname} result] {
  58.         j:alert -text "Can't determine hostname:\n$result"
  59.       } else {
  60.         set hostname $result
  61.       }
  62.     }
  63.   }
  64.   wm client . $hostname
  65. }
  66.  
  67. ######################################################################
  68. # j:wm_command ?args? - set the session client command
  69. ######################################################################
  70.  
  71. proc j:wm_command {{command ""}} {
  72.   global argv0 argv
  73.   
  74.   if {[llength $command] == 0} {
  75.     set command [concat $argv0 $argv]
  76.   }
  77.   
  78.   wm command . $command
  79. }
  80.  
  81. ######################################################################
  82. # j:new_toplevel prefix ?args? -
  83. #   create a new toplevel, avoiding name conflicts
  84. ######################################################################
  85.  
  86. proc j:new_toplevel { prefix args } {
  87.   set count 0
  88.   
  89.   while {[winfo exists $prefix$count]} {
  90.     incr count
  91.   }
  92.   
  93.   set tl $prefix$count
  94.   toplevel $tl
  95.   
  96.   if {"x$args" != "x"} {
  97.     eval [list $tl configure] $args
  98.   }
  99.   return $tl
  100. }
  101.  
  102. ######################################################################
  103. # j:selection_if_any - return selection if it exists, else {}
  104. #   this is from R. James Noble <kjx@comp.vuw.ac.nz>
  105. ######################################################################
  106.  
  107. proc j:selection_if_any {} {
  108.   if {[catch {selection get} s]} {return ""} {return $s}
  109. }
  110.  
  111. ######################################################################
  112. # j:beep w - "ring bell" in widget W
  113. ######################################################################
  114.  
  115. proc j:beep { w } {
  116.   global j_beep J_PREFS
  117.   
  118.   set delay 100                ;# should be a preference
  119.   
  120.   if { ! [info exists j_beep($w)] } {
  121.     set j_beep($w) 0
  122.   }
  123.   
  124.   if $j_beep($w) {
  125.     return 1
  126.   }
  127.   set j_beep($w) 1            ;# used so bells don't queue up
  128.   
  129.   if $J_PREFS(visiblebell) {
  130.     set fg black
  131.     set bg white
  132.     
  133.     if ![catch {set fg [lindex [$w configure -foreground] 4]}] {
  134.       catch {$w configure -foreground $bg}
  135.       after $delay "catch {$w configure -foreground $fg}"
  136.     }
  137.     if ![catch {set bg [lindex [$w configure -background] 4]}] {
  138.       catch {$w configure -background $fg}
  139.       after $delay "catch {$w configure -background $bg}"
  140.     }
  141.     update
  142.     after $delay "
  143.       update
  144.       set j_beep($w) 0
  145.     "
  146.   }
  147.   if $J_PREFS(audiblebell) {
  148.     j:tk4 {bell -displayof $w}
  149.   }
  150.   
  151.   after $delay "set j_beep($w) 0"    ;# allow processing future bells
  152.   
  153.   return 0
  154. }
  155.  
  156. ######################################################################
  157. # j:no_selection - true if there is no selection
  158. ######################################################################
  159.  
  160. proc j:no_selection {} {
  161.   if {[catch {selection get} s]} {return 1} {return 0}
  162. }
  163.  
  164. ######################################################################
  165. # j:default_button button widget... - bind <Return> to default button
  166. #   widget... is one or more widgets that can have the kbd focus
  167. ######################################################################
  168.  
  169. proc j:default_button { button args } {
  170.   foreach w $args {
  171.     bind $w <Return> "$button invoke"
  172.   }
  173. }
  174.  
  175. ######################################################################
  176. # j:cancel_button button widget... - set up bindings for cancel button
  177. #   widget... is one or more widgets that can have the kbd focus
  178. ######################################################################
  179.  
  180. proc j:cancel_button { button args } {
  181.   foreach w $args {
  182.     bind $w <Control-c> "$button invoke"
  183.     bind $w <Control-g> "$button invoke"
  184.     bind $w <Meta-q> "$button invoke"
  185.     bind $w <Meta-period> "$button invoke"
  186.   }
  187. }
  188.  
  189. ######################################################################
  190. # j:tab_ring widget... - bind Tab and Shift-Tab to cycle through widgets
  191. #  widget... is the list of widgets to bind, in order
  192. ######################################################################
  193. ### It's unfortunate to have to hardwire Shift-Tab to Backtab, but there
  194. ### doesn't seem to be a <Backtab> X11 keysym.
  195.  
  196. proc j:tab_ring {args} {
  197.   # index of last widget
  198.   set last [expr {[llength $args] - 1}]
  199.   
  200.   for {set i 0} {$i < $last} {incr i} {
  201.     set this [lindex $args $i]
  202.     set next [lindex $args [expr {$i + 1}]]
  203.     bind $this <Tab> "focus $next"
  204.     bind $next <Shift-Tab> "focus $this"
  205.   }
  206.   
  207.   # ... and bind last to focus on first:
  208.   set this [lindex $args $last]
  209.   set next [lindex $args 0]
  210.   bind $this <Tab> "focus $next"
  211.   bind $next <Shift-Tab> "focus $this"
  212. }
  213.  
  214. ######################################################################
  215. # j:dialogue w - arrange to position window w near ctr of screen
  216. #   mostly borrowed from /usr/local/lib/tk/dialog.tcl
  217. # does nothing unless $J_PREFS(autoposition)
  218. ######################################################################
  219.  
  220. proc j:dialogue { w } {
  221.   global J_PREFS
  222.  
  223.   if $J_PREFS(autoposition) {
  224.     # first, display off-screen:
  225.     wm withdraw $w        ;# hide the window
  226.  
  227.     update idletasks        ;# force geometry managers to run
  228.     # calculate position:
  229.     set x [expr [winfo screenwidth $w]/2 - [winfo reqwidth $w]/2 \
  230.         - [winfo vrootx [winfo parent $w]]]
  231.     set y [expr [winfo screenheight $w]/3 - [winfo reqheight $w]/2 \
  232.         - [winfo vrooty [winfo parent $w]]]
  233.     wm geom $w +$x+$y
  234.     wm deiconify $w
  235.  
  236.     update idletasks        ;# force geometry managers to run
  237.     wm deiconify $w        ;# display window
  238.     wm focus $w
  239.   }
  240. }
  241.  
  242. proc j:dialog [info args j:dialogue] [info body j:dialogue]
  243.  
  244. ######################################################################
  245. # j:rule parent [args] - returns a rule suitable for parent
  246. #       used as argument to a pack command
  247. ######################################################################
  248.  
  249. proc j:rule { {parent {}} args} {
  250.   global j_rule
  251.  
  252.   if {$parent == "."} {set parent ""}    ;# so "." doesn't give "..rule0"
  253.   
  254.   if {[info exists j_rule(count)]} then {
  255.     set j_rule(count) [expr {$j_rule(count) + 1}]
  256.   } else {
  257.     set j_rule(count) 0
  258.   }
  259.  
  260.   set rule "$parent.rule$j_rule(count)"
  261.   frame $rule -class Rule
  262.   if {$args != ""} {eval $rule configure $args}
  263.   return $rule
  264. }
  265.  
  266. ######################################################################
  267. # j:filler parent [args] - returns a filler frame suitable for parent
  268. #       used as argument to a pack command
  269. ######################################################################
  270.  
  271. proc j:filler { {parent {}} args} {
  272.   global j_filler
  273.  
  274.   if {$parent == "."} {set parent ""}    ;# so "." doesn't give "..filler0"
  275.   
  276.   if {[info exists j_filler(count)]} then {
  277.     set j_filler(count) [expr {$j_filler(count) + 1}]
  278.   } else {
  279.     set j_filler(count) 0
  280.   }
  281.  
  282.   set filler "$parent.filler$j_filler(count)"
  283.   frame $filler -class Filler
  284.   if {$args != ""} {eval $filler configure $args}
  285.   return $filler
  286. }
  287.  
  288. ######################################################################
  289. # j:configure_font widget fontlist - use font from list, or default
  290. #   tries to set widget's font to each font in list.
  291. #   if a font is `default', tries to set to X default font.
  292. #   if a font is {}, sets to courier 12-point.
  293. ######################################################################
  294.  
  295. proc j:configure_font { widget fontlist } {
  296.   foreach font $fontlist {
  297.     # try to use each font, until one is successful:
  298.     if {$font == {default}} {
  299.       set font [option get $widget font Font]
  300.       if {$font == {}} {set font {*-courier-medium-r-normal--*-120-*}}
  301.     }
  302.     if {! [catch {$widget configure -font $font}]} {return}
  303.   }
  304. }
  305.  
  306. ######################################################################
  307. # j:configure_tag_font widget tag fontlist - use font from list, or default
  308. #   tries to set tag's font to each font in list.
  309. #   if a font is `default', tries to set to X default font.
  310. #   if a font is {}, sets to courier 12-point.
  311. ######################################################################
  312.  
  313. proc j:configure_tag_font { widget tag fontlist } {
  314.   foreach font $fontlist {
  315.     # try to use each font, until one is successful:
  316.     if {$font == {default}} {
  317.       set font [option get $widget font Font]
  318.       if {$font == {}} {set font {*-courier-medium-r-normal--*-120-*}}
  319.     }
  320.     if {! [catch {$widget tag configure $tag -font $font}]} {return}
  321.   }
  322. }
  323.